package servlet;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;

/**
 * Servlet implementation class ClusterServlet
 */
public class ClusterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public ClusterServlet() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String submitKey = request.getParameter("key");
		String submitValue = request.getParameter("value");	
	    HttpSession s = request.getSession();
	    if(StringUtils.isNotEmpty(submitKey) && StringUtils.isNotEmpty(submitValue)){
	        s.setAttribute(submitKey, submitValue);
	    }
		Map m = new HashMap();
		Enumeration<String> e = s.getAttributeNames();
		while(e.hasMoreElements()){
		    String name = e.nextElement();
		    String value = (String) s.getAttribute(name);
		    m.put(name, value);
		}
		request.setAttribute("map", m); 
		request.getRequestDispatcher("/a.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
